//
//  HJWelcomeViewController.swift
//  zhangmiSinweibo
//
//  Created by zhangmi on 16/5/24.
//  Copyright © 2016年 Paramount Pictures. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class HJWelcomeViewController: UIViewController {

    override func loadView() {
        view = backView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
    }
    
    //设置UI界面
    private func setUpUI() {

        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints{ (make) in
        make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-300)
            //固定图片的大小
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.height.equalTo(80)
            make.width.equalTo(80)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //SDWebImage 框架在3.7.6版本图片会根据屏幕的分辨率进行缩放
        iconView.sd_setImageWithURL(HJUserAccountViewModel.sharedAccountViewModel.headURL)
//        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(HMUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large)
//        //第一次获取不到图片 为nil
//        print(image.scale)

        
        //本地图片设置的圆角
        iconView.layer.cornerRadius = 45
        //设置nameLabel的透明度
        nameLabel.alpha = 0
//      iconView.layer.masksToBounds = true
    }

    //动画效果不建议在viewDidLoad阶段或者之前的调用
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        //弹簧动画效果
        //usingSpringWithDamping: 阻尼系数 0 ~ 1 值越小 效果越弹
        //initialSpringVelocity : 加速度 值越大 速度越快
        //OC:位移枚举 表示多个动画选项 '|' swift: 使用数组数组
        let offset = -ScreenHeight + 500
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: {
            //修改约束
            self.iconView.snp_makeConstraints(closure: { (make) in
                make.bottom.equalTo(400)
            })
            
            //取消之前所有的约束, 再添加新的约束
//            self.iconView.snp_remakeConstraints(closure: { (make) in
//                
//            })
            //更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            
            //提前刷新父视图
            
            self.view.layoutIfNeeded()
            }) { (_) in
                print("👌")
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
                    print("👐")
                        
                    //切换根控制器
//          UIApplication.sharedApplication().keyWindow?.rootViewController = HJMainViewController()
                
                //object 来传递参数
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                })
        }
        
    }
    
    
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    //懒加载几个子视图
    //背景视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"02"))
   
    
    ///用户名称文案
    private lazy var nameLabel: UILabel = UILabel(title: HJUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
    
        
//        {
//        let l = UILabel()
//        print(HJUserAccountViewModel.sharedAccountViewModel.userAccount?.name)
//        l.text = HJUserAccountViewModel.sharedAccountViewModel.userAccount?.name
//        l.textColor = UIColor.darkGrayColor()
//        l.alpha = 0
//        //自适应大小
//        l.sizeToFit()
//        return l
//    }()


}
